package org.lora.smp.approve;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.lora.biz.ou.exception.LoraOrgUserException;
import org.lora.biz.workflow.engines.ExcuterBean;
import org.lora.biz.workflow.engines.IProcessMoveService;
import org.lora.biz.workflow.engines.bean.PathDefinitionBean;
import org.lora.biz.workflow.engines.bean.TaskInstanceBean;
import org.lora.core.annotation.Action;
import org.lora.core.annotation.AutoAssemble;
import org.lora.mvc.annotation.RequestPath;
import org.lora.mvc.annotation.ResponseJSON;
import org.lora.smp.common.CommconSmpAction;
import org.lora.smp.document.DocumentContentEntity;
import org.lora.util.StringUtil;
import org.lora.webframework.common.CommonConstant;
import org.lora.webframework.common.JsonResult;

/**
 * <p>
 * 类名:SmApproveAction
 * </p>
 * 描述:<br>
 * 创建时间:2016年10月31日 上午11:37:58<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年10月31日 Bladnir 创建<br>
 */
@Action
@RequestPath(path = "/smp/approve")
public class SmApproveAction extends CommconSmpAction {

	@AutoAssemble
	private IProcessMoveService processMoveService;

	/**
	 * 表单保存操作
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/saveOperation")
	@ResponseJSON
	public JsonResult saveOperation(HttpServletRequest request) {
		String processDefinitionId = request.getParameter("processDefinitionId");
		String taskId = request.getParameter("taskId");
		String version = request.getParameter("version");

		JsonResult result = new JsonResult();

		result.setFlag(CommonConstant.FAIL);

		try {
			if (StringUtil.isNull(taskId)) {
				ExcuterBean excuterBean = super.getCurrentExcuterBean();
				processMoveService.draft(processDefinitionId, excuterBean, version);
			}

			result.setFlag(CommonConstant.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;

	}

	/**
	 * 表单提交操作
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/submitOperation")
	@ResponseJSON
	public JsonResult submitOperation(HttpServletRequest request) {
		String processDefinitionId = request.getParameter("processDefinitionId");
		String processInstanceId = request.getParameter("processInstanceId");
		String taskId = request.getParameter("taskId");
		String version = request.getParameter("version");

		DocumentContentEntity documentContentEntity = getDocumentContentFromRequest(request);

		JsonResult result = new JsonResult();

		result.setFlag(CommonConstant.FAIL);

		try {

			if (StringUtil.isNull(taskId)) {
				ExcuterBean excuterBean = super.getCurrentExcuterBean();
				processMoveService.draft(processDefinitionId, excuterBean, version);
			} else {

				TaskInstanceBean taskInstanceBean = processMoveService.getTaskInstance(processInstanceId, taskId, false, false);

				excuteProcess(taskInstanceBean, documentContentEntity);

				processMoveService.submit(taskInstanceBean);
			}

			result.setFlag(CommonConstant.SUCCESS);

		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return result;

	}

	private void excuteProcess(TaskInstanceBean taskInstanceBean, DocumentContentEntity documentContentEntity) throws LoraOrgUserException {
		
		 List<PathDefinitionBean> nextPathList =selectPath(taskInstanceBean, documentContentEntity);
		 
		 selectExcuter(taskInstanceBean, nextPathList, documentContentEntity);
		 
		 taskInstanceBean.getActivityDefinitionBean().setNextPathList(nextPathList);
	}

	private List<PathDefinitionBean> selectPath(TaskInstanceBean taskInstanceBean, DocumentContentEntity documentContentEntity) {

		String actNo = taskInstanceBean.getActivityDefinitionBean().getActNo();
		List<PathDefinitionBean> nextPathList = taskInstanceBean.getActivityDefinitionBean().getNextPathList();
		List<PathDefinitionBean> resultList = new ArrayList<>();
		switch (actNo) {
		case "act01":
			resultList.add(nextPathList.get(0));
			break;
		case "act02":
			resultList.add(nextPathList.get(0));
			break;
		case "act03":
			resultList.add(nextPathList.get(0));
			break;
		case "act04":
			resultList.add(nextPathList.get(0));
			break;
		default:
			resultList.add(nextPathList.get(0));
			break;
		}

		return resultList;

	}

	private void selectExcuter(TaskInstanceBean taskInstanceBean, List<PathDefinitionBean> pathList,
			DocumentContentEntity documentContentEntity) throws LoraOrgUserException {

		String actNo = taskInstanceBean.getActivityDefinitionBean().getActNo();

		switch (actNo) {
		case "act01":
			for (PathDefinitionBean pd : pathList) {

				List<ExcuterBean> handerList = new ArrayList<>();
				ExcuterBean excuterBean = super.getExcuterBeanByEiId(documentContentEntity.getTeamLeaderId(),
						documentContentEntity.getDomainId());
				handerList.add(excuterBean);
				pd.getToActivityDefinitionBean().setHanderList(handerList);

			}
			break;
		case "act02":
			break;
		case "act03":
			break;
		case "act04":
			break;
		default:
			break;
		}

	}
}
